<?php
class FormulariosController extends AppController {

	var $name = 'Formularios';
	var $uses = array('Formulario','Typecampo','Campo','Formularioscampo');
	
    function beforeFilter () {
        parent::beforeFilter();
    }
	
	function index() {
        $this->set('title_nav_bar','Formularios','Formularioscampo');
		$this->set('formularios', $this->paginate());
	}


    function edit($id=0) {

		if ($_POST)  {


			if ($_POST['save']=='Agregar') {

				if ($this->data['Formulario']['id']==0){
					$this->Formulario->save($this->data['Formulario']);
					$this->data['Formulario']['id'] = $this->Formulario->getLastInsertID();

				}

				$Formularioscampos['id']            = 0;
				$Formularioscampos['campo_id']      = $this->data['Formularioscampo']['campo_id'];
				$Formularioscampos['label']         = $this->data['Formularioscampo']['label'];
				$Formularioscampos['size']          = $this->data['Formularioscampo']['size'];
				$Formularioscampos['formulario_id'] = $this->data['Formulario']['id'];
				$this->Formularioscampo->save($Formularioscampos);
				$id = $this->data['Formulario']['id'];

			} elseif ( $_POST['save']=='Ordenar' ) {
				
				$ordenar = $_POST['ordenar'];
				foreach($ordenar as $clave=>$valor){
					$this->Formularioscampo->id = $clave;
					$this->Formularioscampo->saveField('orden', $valor);
				}


				$id =  $this->data['Formulario']['id'];
			
			} elseif (isset($this->data['Formulario']['save_apply'])) {

				if ($this->Formulario->save($this->data['Formulario'])) {
					$this->Session->setFlash(__('Guardado correctamente', true));
					$this -> redirect(array('action'=>'index'));
					exit();                
				} else {
					$this->Session->setFlash(__('No se pudo guardar el formulario.', true));
				}  
			}

		} // end if $_POST


            
        if ($id == 0) {
                $this->set('title_nav_bar','Crear un Formulario');
                // Nuevo Formulario
                // obtain default values for new form
                $T = $this->Formulario->_schema;
                foreach($T as $clave => $valor) { $this->data['Formulario'][$clave] = $valor['default']; }
                $this->data['Formulario']['id'] = 0;    
        } else {
                $this->set('title_nav_bar','Editar Formulario (id:'.$id.')');
				$this->Formulario->recursive = 3;
                $this->data= $this->Formulario->read(null, $id);
				
        }

//		$lista_campos = $this->Campo->find('list', array('fields' => array('Campo.label')) );



		$datos_campos = $this->Campo->find('all', array(
							'fields' => array('Campo.id','Campo.label','Typecampo.name')
						));

		foreach ($datos_campos as $key => $registro) {
			$lista_campos[$registro['Campo']['id']] = $registro['Campo']['label']." (".$registro['Typecampo']['name'].")";
		}


		$this->set('lista_campos',$lista_campos);

        $lista_formularios = $this->Formulario->find('list', array('fields' => array('Formulario.name')) );
        $this->set('lista_formularios',$lista_formularios);
        
    }


	function delete() {

		if (empty($this->data['Delete'])) {
			$this->Session->setFlash(__('Debe seleccionar un formulario', true));
			$this->redirect(array('action'=>'index'));
		} else {
 
            foreach($this->data['Delete'] as $key=>$value) {
                if ($value==1) { 
                    $this->Formulario->delete($key,true); 
                }
            }
            $this->Session->setFlash(__('Formulario/s eliminado/s', true));
    		$this->redirect(array('action'=>'index'));

		} 
	}

	
	function eliminarcampo($formulario_id=0,$campo_id=0 ) {

		if ($campo_id==0 OR $formulario_id==0) {
			$this->Session->setFlash(__('Debe seleccionar un campo del formulario', true));
			$this->redirect(array('action'=>'index'));
		} else {
 
            $this->Formularioscampo->delete($campo_id,true); 

            $this->Session->setFlash(__('Campo del Formulario eliminado', true));
    		$this->redirect(array('action'=>'edit/'.$formulario_id));

		} 
	}
	
	
}
